package com.xsjl.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsjl.base.pojo.Label;
import com.xsjl.base.service.LabelService;
import com.xsjl.common.entity.PageResult;
import com.xsjl.common.entity.Result;
import com.xsjl.common.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName LabelController
 * @Description TODO
 * @Author PRETEXT
 * @Date2020/2/27 20:06
 * @Version V1.0
 **/
@RestController
@RequestMapping("/label")
// @CrossOrigin
public class LabelController {
    @Autowired
    private LabelService service;


    @RequestMapping(method = RequestMethod.POST)
    public Result addLabel(@RequestBody Label label) {
        service.saveLabel(label);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @RequestMapping(method = RequestMethod.GET)
    public Result findAllLabel() {
        return new Result(true, StatusCode.OK, "查询成功", service.findAllLabel());
    }

    @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
    public Result findById(@PathVariable String labelId) {
        return new Result(true, StatusCode.OK, "查询成功", service.findById(labelId));
    }

    @RequestMapping(value = "/{labelId}", method = RequestMethod.PUT)
    public Result updateLabel(@PathVariable String labelId, @RequestBody Label label) {
        label.setId(labelId);
        service.updateLabel(label);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(value = "/{labelId}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String labelId) {
        service.deleteById(labelId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findLabel(@RequestBody Label label) {
        List<Label> labelList = service.findLabelList(label);
        return new Result(true, StatusCode.OK, "查询成功", labelList);
    }

    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findLabel(@PathVariable int page, @PathVariable int size, @RequestBody Label label) {
        Page<Label> pageList = service.pageQuery(page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Label>(pageList.getTotal(), pageList.getRecords()));
    }

}
